<template>
  <div class="container" style="padding: 10px">
    <el-card>
      <el-row type="flex">
        <!-- 左边 -->
        <el-col :span="18">
          <el-row>
            <el-col :span="13.5">
              <el-form label-width="80px" :inline="true" ref="formData">
                <el-form-item label="关键字" size="small">
                  <el-input
                    v-model.trim="page.keyword"
                    class="inp"
                    placeholder="根据文章标题搜索"
                  ></el-input>
                </el-form-item>
                <el-form-item label="状态" size="small">
                  <el-select placeholder="请选择" v-model="page.state">
                    <el-option label="已启用" :value="1"></el-option>
                    <el-option label="已禁用" :value="0"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="5">
              <el-button size="small" plain @click="resetForm">清除</el-button>
              <el-button size="small" type="primary" @click="searchBtn"
                >搜索</el-button
              >
            </el-col>
          </el-row>
        </el-col>
        <!-- 右边 -->
        <el-col :span="6">
          <el-row type="flex" justify="end">
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              @click="showEditAddDialog = true"
              >新增技巧</el-button
            >
          </el-row>
        </el-col>
      </el-row>
      <el-alert
        :title="`数据一共 ${counts} 条`"
        show-icon
        :closable="false"
        type="info"
      >
      </el-alert>

      <!-- 表格 -->
      <el-table
        style="margin-top: 15px"
        :header-cell-style="headerClass"
        :data="data"
        v-loading="loading"
      >
        <el-table-column label="序号" width="80" type="index">
        </el-table-column>
        <el-table-column label="文章标题" width="400" prop="title">
          <template v-slot="{ row }">
            {{ row.title }}
            <i v-if="row.videoURL" @click="videoPreview(row.videoURL)" class="el-icon-film"></i>
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits"> </el-table-column>
        <el-table-column label="录入人" prop="username"> </el-table-column>
        <el-table-column label="录入时间" prop="createTime">
          <template v-slot="{ row }">
            {{ row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100" prop="state">
          <template v-slot="{ row }">
            {{ row.state === 0 ? "已禁用" : "已启用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template v-slot="{ row }">
            <el-button type="text" @click="articlePre(row)">预览</el-button>
            <el-button type="text" @click="changeState(row)">{{
              row.state === 1 ? "禁用" : "启用"
            }}</el-button>
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="editArticle(row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="delArticle(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-row type="flex" style="margin-top: 20px" justify="end">
        <el-pagination
          background
          layout="prev, pager, next,sizes,jumper"
          :total="counts"
          @current-change="changePage"
          @size-change="changeSize"
          :page-sizes="[5, 10, 20, 50]"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 预览弹层 -->
    <articlesPreview
      :showPreview.sync="showPreview"
      :articleDetail="articleDetail"
    ></articlesPreview>
    <!-- 修改新增弹层 -->
    <articlesAdd
      :showEditAddDialog.sync="showEditAddDialog"
      @btnCancel="btnCancel"
      :article="article"
      @add-article="list"
      ref="articlesAdd"
    ></articlesAdd>
    <!-- 视频区域 -->
    <div class="video-preview" v-if="videoUrl">
      <div class="close" @click="close"><i class="el-icon-close"></i></div>
      <div class="box" >
        <video :src="videoUrl" autoplay></video>
      </div>
    </div>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/articles'
import articlesPreview from '../components/articles-preview'
import articlesAdd from '../components/articles-add.vue'
export default {
  components: {
    articlesPreview,
    articlesAdd
  },
  data () {
    return {
      counts: 0, // 总条数
      page: { // 请求数据
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      data: [], // 存储文章列表数据
      loading: false, // 控制loading的显示隐藏
      showPreview: false, // 控制预览弹层
      articleDetail: {}, // 存储点击的那一行文章详情
      showEditAddDialog: false, // 新增编辑弹层
      article: { // 添加或编辑文章的数据
        title: '',
        articleBody: '',
        videoURL: ''
      },
      videoUrl: null // 控制视频区域的显示隐藏
    }
  },
  created () {
    this.list() // 获取文章列表数据
  },
  methods: {
    headerClass () { // 表格标题背景颜色
      return 'background:#fafafa'
    },
    async list () { // 获取文章列表数据
      try {
        this.loading = true // 打开loading
        const { data } = await list(this.page)
        this.counts = data.counts
        this.data = data.items
        // 判断是否还有数据
      } catch (error) {
        console.log(error)
      }
      this.loading = false // 关闭loading
    },
    changePage (page) { // 改变页码值时触发
      this.page.page = page // 将最新的页码赋值给data变量
      this.list() // 重新获取数据
    },
    changeSize (size) { // 改变条目数时触发
      this.page.pagesize = size
      this.list()
    },
    async delArticle (row) { // 点击删除
      try {
        await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await remove(row) // 调用删除接口
        if (this.data.length === 1) { // 判断删除的是不是最后一条数据,如果是让页码值减一
          this.page.page = this.page.page > 1 ? this.page.page - 1 : 1
        }
        this.list() // 重新获取数据
        this.$message.success('删除成功') // 成功提示
      } catch (error) {
        console.log(error)
      }
    },
    async changeState (row) { // 改变状态时触发
      try {
        row.state = row.state === 1 ? 0 : 1
        await changeState(row) // 调用改变状态接口
        this.list() // 重新获取数据
        this.$message.success('操作成功') // 成功提示
      } catch (error) {
        console.log(error)
      }
    },
    async articlePre (row) { // 点击预览
      // 显示弹层
      this.showPreview = true
      this.articleDetail = row
    },
    editArticle (row) { // 点击修改
      this.showEditAddDialog = true
      this.article = row // 数据回显
    },
    resetForm () { // 点击清除
      this.page.keyword = ''
      this.page.state = null
      this.list() // 重新获取数据
    },
    searchBtn () { // 点击搜索
      this.list() // 重新获取数据
    },
    videoPreview (videoUrl) { // 点击图标 视频区域显示
      this.videoUrl = videoUrl
    },
    close () { // 点击关闭 视频区域隐藏
      this.videoUrl = null
    },
    btnCancel () {
      this.article = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    }
  }

}
</script>

<style scoped lang='scss'>
.container {
  padding: 10px;
  .inp {
    width: 190px;
  }
  .el-icon-film {
    font-size: 18px;
    color: #0000ff;
  }
  .video-preview {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    overflow: hidden;
    z-index: 9999;
    .close {
      width: 50px;
      height: 50px;
      position: absolute;
      top: 30px;
      left: 50%;
      -webkit-transform: translate(-50%);
      transform: translate(-50%);
      background: rgba(0, 0, 0, 0.4);
      -webkit-box-shadow: 0 0 5px rgb(0 0 0 / 40%);
      box-shadow: 0 0 5px rgb(0 0 0 / 40%);
      border-radius: 50%;
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-size: 20px;
    }
    .box {
      width: 800px;
      height: 600px;
      position: absolute;
      left: 50%;
      top: 50%;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
      video {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
